<script setup lang="ts">
import {onMounted, ref} from "vue";
import {emitter} from '@/utils/mitt'

const text = ref('加载中...')
const loading = ref(false)

const show = () => {
  loading.value = true
}

const hide = () => {
  loading.value = false
}

const onMittEvent = () => {
  emitter.on('showLoading', (options) => {
    text.value = options.text || '加载中...'
    show()
  })
  emitter.on('hideLoading', hide)
}

onMounted(() => {
  onMittEvent()
})
</script>

<template>
  <div class="global-loading" v-show="loading">
    <div v-loading.lock="loading" element-loading-background="transparent"
         class="loading-lock"/>
    <span class="ml12">{{ text }}</span>
  </div>
</template>
<style scoped>
.global-loading {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  background-color: var(--el-bg-color);
  box-shadow: var(--el-box-shadow);
  z-index: 100;
  color: var(--el-color-primary);
  min-width: 200px;
  max-width: 50%;
  padding: 3px 30px 3px 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
}

.global-loading .loading-lock {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  transform: scale(0.6);
}
</style>
